package crawer.datatransaction.extend;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Hashtable;
import org.jdom.Element;

import crawer.datatransaction.basic.Pattern;
import crawer.datatransaction.basic.PatternFactory;
import crawer.datatransaction.util.*;
import crawer.util.LogCode;
import crawer.util.RegexUtil;


public class Count extends Pattern {
	public void finalize()
	{
		instanceCount--;
	}
	 public Count(PatternFactory p_Factory){
		 super(p_Factory);		
		 super.userObject = new NodeData(Data.Count,Data.Count, NodeData.WithRetrunValue);
		 
		 //name
		 DefaultMutableTreeNode _node = new DefaultMutableTreeNode();
		  _node.setUserObject(new NodeData(Data.Name,Data.Name,NodeData.Leaf));
		 super.AddAttribution(_node);
		 
		 //object
		 _node = new DefaultMutableTreeNode();
		  _node.setUserObject(new NodeData(Data.Object,Data.Object,NodeData.Leaf));
		 super.AddAttribution(_node);	 
		 
		 //case
		 _node = new DefaultMutableTreeNode();
		  _node.setUserObject(new NodeData(Data.Case,Data.False,NodeData.Leaf));
		 super.AddAttribution(_node);
		 
		 //is_Regular
		 _node = new DefaultMutableTreeNode();
		 _node.setUserObject(new NodeData(Data.Regular, Data.False, NodeData.Leaf));
		 super.AddAttribution(_node);
	 }
	 
	 public boolean LoadSetting(Element elt) {		 
		Element sub_elt = null;
		DefaultMutableTreeNode sub_node = null;		
		try{		
		//name
		sub_elt = super.getSingleElement(elt, Data.Name);
		String value = super.getElementValue(sub_elt);
		sub_node = super.getSingleNodebyName(Data.Name);
		super.getNodeData(sub_node).setText(value);

		//object
		sub_elt = super.getSingleElement(elt, Data.Object);
		value = super.getElementValue(sub_elt);
		sub_node = super.getSingleNodebyName(Data.Object);
		super.getNodeData(sub_node).setText(value);

		//Case
		sub_elt = super.getSingleElement(elt, Data.Case);
		if (sub_elt != null) {
			value = super.getElementValue(sub_elt);
			sub_node = super.getSingleNodebyName(Data.Case);
			super.getNodeData(sub_node).setText(value);
		}

		//Regular
		sub_elt = super.getSingleElement(elt, Data.Regular);
		if (sub_elt != null) {
			value = super.getElementValue(sub_elt);
			sub_node = super.getSingleNodebyName(Data.Regular);
			super.getNodeData(sub_node).setText(value);
		}
		}catch(Exception e){
			log.info(e);
		}
		return super.LoadSetting(elt);
	}
	 
	 public  Object Operate(String p_DataStream, String p_strUrl, Hashtable p_PreData) {
		 AnElement _element = new AnElement();
		 try{
		 _element.name = super.getNodeData(this.getSingleNodebyName(Data.Name)).getText();
		 _element.type = Data.Int32;
		 
		 String _strObject = super.getNodeData(this.getSingleNodebyName(Data.Object)).getText();
		 String  str_regular = super.getNodeData(this.getSingleNodebyName(Data.Regular)).getText();
		 String  str_case = super.getNodeData(this.getSingleNodebyName(Data.Case)).getText();
		 
		 boolean is_regular = false;
		 boolean case_sense = false;
		 
		 if(str_regular.equalsIgnoreCase(Data.True)){
			 is_regular = true;
		 }
		 if(str_case.equalsIgnoreCase(Data.True))
			 case_sense = true;
		 _element.value = Integer.toString(RegexUtil.matchCount(p_DataStream, _strObject, is_regular, case_sense));
		 }catch(Exception e){
				log.error("[["+LogCode.E_COUNT_FAIL+"]], URL:["+ p_strUrl + "]");
		 }
		 return _element;
     }
	 
	 

}
